from django.shortcuts import render
from datetime import datetime

# Create your views here.
from first.models import PurchaseM, PurchaseDetail
from first.Serial import PurchaseMSerializer, MsgSerializer, PurchaseMModelSerial, PDMSerializer, PurchaseMDModelSerial, PurchaseMDModelSerialDB
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from django.http import HttpResponse
from rest_framework.views import APIView
from io import BytesIO


class PDM(APIView):
    def get(self, request):
        MM = PurchaseDetail.objects.all()
        Sdetail = PDMSerializer(instance=MM, many=True)
        json = JSONRenderer().render(Sdetail.data)
        return HttpResponse(json, content_type='application/json')


class PurchaseMV(APIView):
    def get(self, request):
        MM = PurchaseM.objects.all()
        for one in MM:
            print(one)
        Sdetail = PurchaseMModelSerial(instance=MM, many=True)
        json = JSONRenderer().render(Sdetail.data)
        print(json)
        return HttpResponse(json, content_type='application/json')


def GetPurchaseM(request):
    if request.method == 'GET':
        purchaseM = PurchaseM.objects.get(id=1)
        serial = PurchaseMSerializer(instance=purchaseM, many=False)
        print(serial.data)
        json = JSONRenderer().render(serial.data)
        # print(json)
        return HttpResponse(json, content_type='application/json')
        # return HttpResponse(serial.data, content_type='application/json')


class Message(object):
    def __init__(self, author, title, content, Buildtime=None):
        self.author = author
        self.title = title
        self.content = content
        self.Buildtime = Buildtime or datetime.now()


msg1 = Message(author='sanku', title='buy fruits', content='1000kg coconut')
msg2 = Message(author='sanku', title='buy fruits', content='1000kg apple')
msg = [msg1, msg2]


def GetMsg(request):
    if request.method == 'GET':
        Msg = MsgSerializer(instance=msg, many=True)
        print(Msg.data)
        msgs = JSONRenderer().render(Msg.data)
        return HttpResponse(msgs, content_type='application/json')


def ShowDModelSerial(request):
    if request.method == 'GET':
        Detail = PurchaseDetail.objects.filter(id__gte=1)
        detail = PurchaseMDModelSerial(instance=Detail, many=True)
        sdata = JSONRenderer().render(detail.data)
        print('JSON DATA')
        print(sdata)
        data = JSONParser().parse(BytesIO(sdata))
        print('python data')
        print(data)
        return HttpResponse(sdata, content_type='application/json')


def TestPMDB(request):
    data = {'id': 10, 'title': 'purchase bill', 'buyer': 'sanku', 'supplier': 'TOM', 'call': '400123456', 'time': datetime.now()}
    print(data)
    dbData = PurchaseM.objects.filter(id=data['id'])
    print(dbData)
    DBS = PurchaseMDModelSerialDB(data=data)
    print(DBS)
    if DBS.is_valid():
        if dbData:
            DBS.update(instance=dbData, validated_data=data)
        else:
            DBS.save()
        return HttpResponse(data)
    else:
        return HttpResponse(data)
    return HttpResponse(DBS.errors, status=404)
